package com.leyou.item.controller;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.security.acl.Group;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-04-02 20:26
 */

@RestController
@RequestMapping("spec")
public class SpecGroupController {

    @Autowired
    private SpecGroupService specGroupService;

    @Autowired
    private SpecParamService specParamService;

    @PutMapping("group")
    public ResponseEntity<Void> updataGroup(SpecGroup specGroup){
        int i = specGroupService.updateGroup(specGroup);
        if (i<=0){
            ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok().build();
    }

    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> getGroups(@PathVariable("cid") Long cid){
        List<SpecGroup> list = specGroupService.querySpecGroupByCid(cid);
        if (CollectionUtils.isEmpty(list)){
          return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);
    }
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> getSpecParam(
            @RequestParam(value = "gid",required = false) Long gid,
            @RequestParam(value = "cid",required = false) Long cid,
            @RequestParam(value = "searching",required = false) Boolean searching,
            @RequestParam(value = "generic",required = false) Boolean generic
    ){
        List<SpecParam> list = specParamService.querySpecParamByGid(gid,cid,searching,generic);
        if (CollectionUtils.isEmpty(list)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);
    }

    @PostMapping("group")
    public ResponseEntity<Void> addGroup(SpecGroup specGroup){
        int i = specGroupService.insertGroup(specGroup);
        if (i<=0){
            ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("group/{id}")
    public ResponseEntity<Void> deleteGroup(@PathVariable("id") Long id){
        int i = specGroupService.deleteByPKId(id);
        if (i<=0){
            ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok().build();
    }

    @PostMapping("param")
    public ResponseEntity<Void> addParam(SpecParam specParam){
        int i = specParamService.insertParam(specParam);
        if (i<=0){
            ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok().build();
    }

    @PutMapping("param")
    public ResponseEntity<Void> updataParam(SpecParam specParam){
        int i = specParamService.updateParam(specParam);
        if (i<=0){
            ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("param/{id}")
    public ResponseEntity<Void> deleteParam(@PathVariable("id") Long id){
        int i = specParamService.deleteByPKId(id);
        if (i<=0){
            ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok().build();
    }

    @GetMapping("groupAndParam/{id}")
    public ResponseEntity<List<SpecGroup>> groupAndParam(@PathVariable("id") Long id){
       List<SpecGroup> groupAndParam = specGroupService.getGroupAndParam(id);
        return  ResponseEntity.ok(groupAndParam);
    }


}
